import { FrontSide, Mesh, MeshPhysicalMaterial, PlaneGeometry, RepeatWrapping, TextureLoader } from "three";
import plane from '@/assets/imgs/plane.jpg';
import { MyThings } from "@/interfaces/myThings.interface";

class MainPlane implements MyThings {
  mesh: Mesh;
  loaded: Promise<void>;
  constructor() {
    const planeGeometry = new PlaneGeometry(500, 500, 50, 50);
    const plane = new Mesh(planeGeometry);
    plane.rotateX(-Math.PI / 2);
    plane.position.set(0, 0, 0);
    this.mesh = plane;
    this.mesh.name = "ground";
    this.loaded = this.init();
  }
  async init() {
    const textureLoader = new TextureLoader();
    const texture = await textureLoader.loadAsync(plane);
    texture.wrapS = RepeatWrapping;
    texture.wrapT = RepeatWrapping;
    texture.repeat.set(10, 10);
    this.mesh.material = new MeshPhysicalMaterial({
      map: texture,
      shadowSide: FrontSide,
    });
    this.mesh.position.set(0, 0, 0);
    this.mesh.castShadow = true;
    this.mesh.receiveShadow = true;
  }
  update: () => void;
}

export const mainPlane = new MainPlane();
